//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace TestScenarios
{
    using System;
    using System.Web;
    using Preflight;
    using Preflight.Model;
    using Preflight.ServiceModel;

    public static class InitializeScenario
    {
        public static void InitializeAspScenario(this HttpApplication application, AspNetScenarioName scenario)
        {
            switch (scenario)
            {
                case AspNetScenarioName.AspNetCustomAuth:
                    InitializePreflightWithCustomSecurity();
                    break;
                case AspNetScenarioName.AspNetNoSecurity:
                    InitializePreflightUnsecure();
                    break;
                case AspNetScenarioName.AspNetPreflightAuth:
                    InitializePreflightWithSecurity();
                    break;
                case AspNetScenarioName.AspNetWindowsAuth:
                    InitializePreflightWithWindowsAuth();
                    break;
                case AspNetScenarioName.AspNetWindowsAuthDeny:
                    InitializePreflightWithWindowsAuthDeny();
                    break;
                default:
                    throw new Exception("Unsupported scenario!");
            }    
        }

        public static void InitializePreflightUnsecure()
        {
            var preflightServiceOptions = new PreflightServiceOptions { SecurityMode = PreflightServiceSecurityMode.None };
            InitializeService(preflightServiceOptions);
        }

        public static void InitializePreflightWithWindowsAuth()
        {
            var preflightServiceOptions = new PreflightServiceOptions
                {
                    SecurityMode = PreflightServiceSecurityMode.WindowsAuthentication,
                    Roles = new [] { @"BUILTIN\Administrators" }
                };
            InitializeService(preflightServiceOptions);
        }

        public static void InitializePreflightWithWindowsAuthDeny()
        {
            var preflightServiceOptions = new PreflightServiceOptions
            {
                SecurityMode = PreflightServiceSecurityMode.WindowsAuthentication,
                Roles = new[] { @"GroupDoesNotExist" }
            };
            InitializeService(preflightServiceOptions);
        }

        public static void InitializePreflightWithSecurity()
        {
            var preflightServiceOptions = new PreflightServiceOptions
                {
                    SecurityMode = PreflightServiceSecurityMode.PreflightSecurity,
                    CertificateLocation = Constants.Certificate
                };

            InitializeService(preflightServiceOptions);
        }

        public static void InitializePreflightWithCustomSecurity()
        {
            var preflightServiceOptions = new PreflightServiceOptions
            {
                SecurityMode = PreflightServiceSecurityMode.PreflightSecurity,
                CustomAuthorization = new CustomAuthorization()
            };

            InitializeService(preflightServiceOptions);
        }

        public static IPreflightSelfHost CreateAcsSecuredHost()
        {

            var options = PreflightSelfHostedServiceOptions.Create(Constants.AcsAuthServicePort);
            options.SecurityMode = PreflightServiceSecurityMode.ACSSimpleWebToken;
            options.AcsOptions = new AcsOptions
                {
                    Audience = ACSSettings.Realm,
                    Issuer = ACSSettings.AcsUri,
                    SigningKey = ACSSettings.SigningKey
                };
            

            var engineOptions = new PreflightOptions
            {
                EnableResultCaching = true
            };
            var context = new PreflightContext(CreateFactory(), engineOptions);
            var preflightHostConfiguration = PreflightSelfHostConfiguration.Create(context, options);
            return PreflightSelfHost.Create(preflightHostConfiguration);
        }

        public static IPreflightSelfHost CreateSecureHostWithWindowsAuth()
        {
            var options = PreflightSelfHostedServiceOptions.Create(Constants.WinAuthServicePort);
            options.SecurityMode = PreflightServiceSecurityMode.WindowsAuthentication;
            options.CertificateLocation = Constants.Certificate;
            options.Roles = new[] { @"BUILTIN\Administrators" };

            var engineOptions = new PreflightOptions
            {
                EnableResultCaching = true
            };
            var context = new PreflightContext(CreateFactory(), engineOptions);
            var preflightHostConfiguration = PreflightSelfHostConfiguration.Create(context, options);
            return PreflightSelfHost.Create(preflightHostConfiguration);
        }

        public static IPreflightSelfHost CreateSecureHost()
        {
            var options = PreflightSelfHostedServiceOptions.Create(Constants.ServicePort);
            options.SecurityMode = PreflightServiceSecurityMode.PreflightSecurity;
            options.CertificateLocation = Constants.Certificate;

            var engineOptions = new PreflightOptions
                {
                    EnableResultCaching = true
                };
            var context = new PreflightContext(CreateFactory(), engineOptions);
            var preflightHostConfiguration = PreflightSelfHostConfiguration.Create(context, options);
            return PreflightSelfHost.Create(preflightHostConfiguration);
        }

        private static void InitializeService(PreflightServiceOptions serviceOptions)
        {
            var context = new PreflightContext(CreateFactory(), new PreflightOptions());
            PreflightService.Initialize(context, serviceOptions);
        }

        private static IVerificationFactory CreateFactory()
        {
            return new AcceptanceTestsVerificationFactory();
        }
    }
}
